﻿package code.admin 
{


	import caurina.transitions.*;	
	import flash.display.*;
	import flash.events.*;	
	import gemini.utils.*;
	import flash.geom.*

	public class Admin extends AdminDispatcher
	{

		
		private var _userimg;
		private var _adm=0;
		private  var _arr;
		
		public var path="../";
		private var _palette;
		private var _portrait;
		var obj;
		public function Admin()
		{
			
			//portrait=new userport();
		
			_portrait=new geminilogo();
			addChild(_portrait);
			//gotoAndStop("off");
			trace("fghfghfgh");
			
			//init();
		}
		public function init()
		{
			
			obj={}
			obj.title={};
			obj.info={};
			obj.title.hr="";
			obj.title.eng="Click to edit";
			
			obj.info.eng="Editing is still in a production phase and it is available only for demonstration purposes. Any changes will not be saved."
			
			_portrait.btn.addEventListener(MouseEvent.MOUSE_DOWN,adminMe);
			_portrait.btn.addEventListener(MouseEvent.MOUSE_OVER,overPort);
			_portrait.btn.addEventListener(MouseEvent.MOUSE_OUT,outPort);
		}
		private function addImg(e)
		{
			var scale;
			if(_userimg.width>_userimg.height)
			{
				scale=50/_userimg.height;
			}
			else
			{
				scale=50/_userimg.width;
			}
			_userimg.scaleX=scale;
			_userimg.scaleY=scale;
			_userimg.smoothing=true;
			_portrait.holder.addChild(_userimg);
		}
		private function overPort(e)
		{
					
			_mainframe.showMediumInfo(obj);
		}
		private function outPort(e)
		{
			_mainframe.hideInfo();
		}
		public function adminMe(e)
		{
			trace("starting administration");
			if(_adm)
			{
				
				endAdmin(null);
			}
			else
			{
				startAdmin(null);
			}
		}
		override public function startAdmin(e)
		{
			if(!_adm)
			{
				trace("** START ADMIN **");
				
				
				_mainframe.back.addEventListener(MouseEvent.MOUSE_DOWN,_mainframe.dragBack);
				_mainframe.back.addEventListener(MouseEvent.MOUSE_UP,_mainframe.dontDragBack);
				
				outPort(0)
				
				obj.title.hr="Stisni za izlaz";
				obj.title.eng="Click to close administration";
					
				
				_portrait.gotoAndStop("on");
				
				/*
				*
				*
				*/
				_modules=[];
				_modules[0]=new ModuleList(this);
				_modules[1]=new BackgroundList(this);
				
				
				
				_palette=new Palette(this);
				var p=new Point(100,100);
				p=globalToLocal(p);
				_palette.x=p.x;
				_palette.y=p.y;
				addChild(_palette);
				/*
				*
				*
				*/
				for(var m=0;m<_modules.length;m++)
				{
					
					_palette.addModule(_modules[m]);
				}
				
				//
				//
				///
				
				
				
				_adm=1;
				//moveBtns(80);
				
				_arr=[];
				for(var n=0;n<_mainframe.mods.length;n++)
				{
					_arr[n]=new arrows();
					_arr[n].addEventListener(MouseEvent.MOUSE_DOWN,dragMe);
					_arr[n].addEventListener(MouseEvent.MOUSE_UP,dontDragMe);
					_mainframe.mods[n].addChild(_arr[n]);
				}
			
			}
			super.startAdmin(e=null);
			
			
		}
		public function addArrow(clp)
		{
			var n=_arr.length;
			_arr[n]=new arrows();
			_arr[n].addEventListener(MouseEvent.MOUSE_DOWN,dragMe);
			_arr[n].addEventListener(MouseEvent.MOUSE_UP,dontDragMe);
			clp.addChild(_arr[n]);
		}
		override public function endAdmin(e)
		{
			if(_adm)
			{
				trace("** END ADMIN **");				
				
				
				_mainframe.back.removeEventListener(MouseEvent.MOUSE_DOWN,_mainframe.dragBack);
				_mainframe.back.removeEventListener(MouseEvent.MOUSE_UP,_mainframe.dontDragBack);
				
				
				
				obj.title.hr="";
				obj.title.eng="Click to edit";			
				
				outPort(0)
				_portrait.gotoAndStop("off");
				//moveBtns(-80);
				_adm=0;
				for(var n=0;n<_mainframe.mods.length;n++)
				{
					
					_mainframe.mods[n].removeChild(_arr[n]);
				}
				
				removeChild(_palette);
				_palette=null;
				
				_modules=null;;
				_arr=[];
				
				//
				
			}
			super.endAdmin(e=null);
		}
		private function dragMe(e)
		{
			var clp=e.target.parent.parent
			clp.startDrag(false);
			
		}
		private function dontDragMe(e)
		{
			var clp=e.target.parent.parent
			clp.stopDrag();
			trace("\n------\nCLP COORDS: "+clp.x,clp.y);
			_mainframe.makeBackground()
		}
		/*
		*
		*
		*
		*/
		override public function Selected(e)
		{
			super.Selected(e);
		}
		override public function setBackground(lnk)		
		{
			_mainframe.setBackimg(lnk);			
			super.setBackground(this);
		}
		override public function openAdminModule(e)
		{
			_modules[Number(e.module)].openModule();
			super.openAdminModule(_modules[Number(e.module)]);
		}
		override public function closeAdminModule(mod)
		{
			super.closeAdminModule(mod);
		}
		override public function moduleAdminItem(mod)
		{
			mod=_modules[mod.moduleNum].getItem(mod.item);
			super.moduleAdminItem(mod)
		}
		override public function getAdminModule(e)
		{
			
			super.getAdminModule(_modules[Number(e.mod)]);
		}
		
		/*
		*
		*
		*
		*/
		private function moveBtns(num,oc=null)
		{
			for(var n=0;n<_mainframe.nav.btns.length;n++)
			{
				Tweener.addTween
				(
				 	_mainframe.nav.btns[n],
					{
						x:_mainframe.nav.btns[n].x+num,
						time:.3,
						transition:"easeInOutQuad",
						onComplete:function (){if(oc!=null){oc()}}
					}
				 
				 )
			}
		}
		/*public function get mainframe()
		{
			return _mainframe;
		}*/
		
		
	}
}
